#coding=utf-8
import requests,DBUtil,json
_url = "http://api.tianapi.com/meinv/?key=32b8424790782df81cc4d9ba42f3b327&num=10"

photo = {}
photoList = []
def getData(source):
    r = requests.get(_url)
    # r.encoding='utf-8'
    data = r.json()
    list = data['newslist']
    if len(list)>0:
        for item in list:
            photo['photo_title'] = item['title']
            photo['description'] = item['description']
            photo['picUrl'] = item['picUrl']
            photo['photo_source'] = source
            print(photo)
            photoList.append(photo)
    return photoList
def saveData(list):
    dbutil = DBUtil.dbmysql()
    params = []
    for m in list:
        str = (m['photo_title'], m['description'], m['picUrl'], m['photo_source'])
        params.append(str)
   # dbutil.batchInsert("insert into movie(name, img) values (%s, %s)", params)
    dbutil.batchInsert("insert into photo(photo_title, description, picUrl, photo_source) values (%s, %s, %s, %s)", params)


# 爬虫开始
# __list = getData('tianxing_meinv')
# saveData(__list)
#清空数据

dbutil = DBUtil.dbmysql()
dbutil.delete(" delete  from photo where photo_source='tianxing_meinv'")
limit = 1
while True:
    try:
        # for ii in range(limit):
        saveData(getData('tianxing_meinv'))
        limit +=1
    except  Exception as e:
        print("爬取失败。。。")
    if limit ==20: # 爬200条
        break
print(dbutil.query(" select count(*) from photo"),end="条数据")
